//
//  TTDianpuCommonViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/11/6.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuCommonViewCell.h"
#import "TTDianpuTextCellModel.h"
#import "TTDianpuChooseCellModel.h"
#import "TTDianpuSwitchCellModel.h"

@interface TTDianpuCommonViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
@property (weak, nonatomic) IBOutlet UIImageView *rightImageView;
@property (weak, nonatomic) IBOutlet UIButton *topBtn;


@end

@implementation TTDianpuCommonViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
    
    @weakify(self)
    [self.textfield.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        self.model.content = x;
    }];
    
}

// 点击了覆盖的按钮
- (IBAction)clickTopBtn:(UIButton *)sender {
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    TTDianpuChooseCellModel *chooseModel = (TTDianpuChooseCellModel *)self.model;
    if (chooseModel.target && chooseModel.selector) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [chooseModel.target performSelector:chooseModel.selector withObject:chooseModel];
#pragma clang diagnostic pop
    }
}

- (IBAction)clickSwitch:(UISwitch *)sender {
    
    TTDianpuSwitchCellModel *chooseModel = (TTDianpuSwitchCellModel *)self.model;
    
    chooseModel.on = sender.on;
    
    if (chooseModel.switchBlock) {
        chooseModel.switchBlock(chooseModel);
    }
    
}

- (void)setModel:(TTDianpuCellModel *)model canEdit:(BOOL)canEdit
{
    [self setModel:model];
    
    self.textfield.enabled = canEdit;
    self.topBtn.enabled = canEdit;
    
}


- (void)setModel:(TTDianpuCellModel *)model
{
    _model = model;
    self.titleLabel.text = model.title;
    self.textfield.placeholder = model.placeholder;
    self.textfield.text = model.content;
    [self.textfield setPlaceholder:model.placeholder lightColor:UIColorHex(999999) darkColor:UIColorHex(999999)];
    
    if ([model isKindOfClass:[TTDianpuTextCellModel class]]) {
        
        [self setTextCellModel];
        
    } else if ([model isKindOfClass:[TTDianpuChooseCellModel class]]) {
        
        [self setChooseCellModel];
        
    } else if ([model isKindOfClass:[TTDianpuSwitchCellModel class]]) {
           
        [self setSwitchCellModel];
        
    } else {
        
        [self setTextCellModel];
    }
    
}

- (void)setTextCellModel
{
    self.topBtn.hidden = YES;
    self.textfield.enabled = YES;
    self.rightImageView.hidden = YES;
    self.mySwitch.hidden = YES;
}

- (void)setChooseCellModel
{
    TTDianpuChooseCellModel *chooseModel = (TTDianpuChooseCellModel *)self.model;
    self.topBtn.hidden = NO;
    self.textfield.enabled = NO;
    self.mySwitch.hidden = YES;
    self.rightImageView.hidden = NO;
    NSString *imageName = chooseModel.rightImageName ? : @"order_right_arrow";
    self.rightImageView.image = [UIImage imageNamed:imageName];
    
}


- (void)setSwitchCellModel
{
    TTDianpuSwitchCellModel *switchModel = (TTDianpuSwitchCellModel *)self.model;
    
    self.topBtn.hidden = YES;
    self.textfield.enabled = NO;
    self.rightImageView.hidden = YES;
    self.mySwitch.hidden = NO;
    
    self.mySwitch.on = switchModel.on;
    
}



@end
